<?php

namespace App\Http\Resources\Admin;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;

class SysUserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        // 在列表中显示的字段
        $data = [
            'id' => $this->id,
            'name' => $this->name,
            'username' => $this->username,
            'nickname' => $this->nickname,
            'status' => $this->status,
            'email' => $this->email,
            'mobile' => $this->mobile,
            'department_id' => $this->department_id,
            'genderLabel' => $this->gender == 1 ? '男' : ($this->gender == 0 ? '女' : '未知'),
            // 在列表中显示但在详情中隐藏的字段
            'createdAt' => $this->when($request->routeIs(['sys:user:list']), function () {
                // Log::info('created_at: '. $this->created_at);
                return $this->created_at->format('Y-m-d H:i:s');
            }),
            'updatedAt' => $this->when($request->routeIs(['sys:user:list']), function () {
                return $this->updated_at->format('Y-m-d H:i:s');
            }),
            // 'perms' =>  $this->when($request->routeIs('sys:user:list'), function () {
            //     return $this->perms;
            // }),
        ];

        return $data;
    }
}
